<?php

/**
 * @Author: liu
 * @Date:   2018-07-25 14:46:28
 * @Last Modified by:   liu
 * @Last Modified time: 2018-09-13 16:20:47
 * 改脚本只处理 预存款的各种逻辑
 * 该类型凭证是运行所有指定的 门店一天的组合 之后推送所以 一定注意本凭证 同门店同一天 不能重复推送 
 */
$api_service_path = __DIR__ . "/../../../../api/";
ob_start();
require_once($api_service_path . "public/index.php");
ob_end_clean();
DB::connection()->disableQueryLog(); 
/**
 * 用于金蝶推送账套的脚本每月执行一次
 */

// $mdb=MaintenanceShops::where("status",1)->get();//循环各个门店的入库出库各种记录
// $_date=array(date('Y-m-01', strtotime('-1 month')),date('Y-m-t', strtotime('-1 month')));

// $mdb=MaintenanceShops::where("status",1)->where("id","!=",5)->get();
$mdb=MaintenanceShops::whereIn("id",array(1))->get();
// $_date=array("2018-07-01","2018-07-31");
// $mdb=MaintenanceShops::where("status",1)->where("id","!=",5)->get();//循环各个门店的入库出库各种记录
$c_date=date('Y-m-d',time()-86400);//前一天
$_date=array($c_date,$c_date);
$_date=array("2018-09-01","2018-09-01");

Log::useFiles(storage_path().'/logs/kingdee_push.log');

$temp_shop_array=array();
$temp_mdb=MaintenanceShops::where("status",1)->get();
foreach ($temp_mdb as $_m) {
	$temp_shop_array[$_m->id]=array();
}    
$cur_date=floor((strtotime($_date[1])-strtotime($_date[0]))/86400);
for($i=0;$i<=$cur_date;$i++){   
	$data=array('mainVoucher' =>
	    array(
	            
	    )
	);
	$entty_array=array();//组织所有的最基层 结构
	$inputjie = array(
	    "FAccountID"=>"1243",
	    "FExplanation"=>"",
	    "FCurrencyID"=>1,
	    "FExchangeRate"=>1,
	    "FDC"=>1,
	    "FAmountFor"=>100,
	    "FAmount"=>100,
	    "data"=>array(
	        "009"=>"114",//商品分类
	        "012"=>"001",//直营店
	    ),
	);
	$inputdai = array(
	    "FAccountID"=>"1243",
	    "FExplanation"=>"脚本测试1",
	    "FCurrencyID"=>1,
	    "FExchangeRate"=>1,
	    "FDC"=>0,
	    "FAmountFor"=>100,
	    "FAmount"=>100,
	    "data"=>array(
	        "009"=>"114",//商品分类
	        "012"=>"001",//直营店
	    ),
	);
	$shop_array=$temp_shop_array;//用于门店的循环数组 获取当天的所有门店的  根据门店处理的凭证数组
    $date=date('Y-m-d',strtotime($_date[0])+$i*86400); 
    //第一遍循环 用于获取所有企业当天的 所有来往款项
    foreach ($mdb as $_mdb) {//
        if(!$_mdb->kingdee_groupid){
            Log::error("门店 ".$_mdb->id."金蝶科目部门异常");
            continue 2; //跳过该天所有门店的
        }       
        $ower_msk=MaintenanceShopKingdee::where("shop_id",$_mdb->id)->where("code","1133.01")->first();//本门店的现金账套
        if(is_null($ower_msk)){
            Log::error("门店 ".$_mdb->id."内部往来科目异常");
            continue 2;//有错误数据 跳出当天的 查询
        }
        $begin_date=date("Y-m-d H:i:s",strtotime($date." 00:00:00")-Config::get("config.report_time_diff"));
        $end_date=date("Y-m-d H:i:s",strtotime($date." 23:59:59")-Config::get("config.report_time_diff"));
                
        //订单余额冲减收入
        $o_amount=DB::select("SELECT opr.id,opr.amount,u.kingdee_code,u.phone,u.type,u.real_name,o.car_no
			FROM order_pay_record opr
			LEFT JOIN orders o ON o.order_id = opr.order_id
			LEFT JOIN users u ON u.id = o.pay_user_id
			WHERE o.`status` IN (1018, 1017, 1217, 1317, 1417)
			AND o.pay_time BETWEEN ? AND ?  AND o.shop_id = ?
			AND opr.pay_type = 8
			AND opr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        foreach ($o_amount as $_oa) {
        	$urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type = 1 group by uad.acount_type,aurr.shop_id", array($_oa->id));//获取这笔支付的完成需要几笔 充值支付记录
			$cur_type=$_oa->type==1?"个人":"企业";
			$cur_name=$_oa->real_name;
			$cur_code=$_oa->kingdee_code;			
			$car_no=$_oa->car_no;
			$user_type=$_oa->type;
			$flag=getlase($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code);
			if(!$flag){
				continue 3; //跳过该天所有门店的
			}			
        }
        //洗车现金收入
        $c_amount=DB::select("SELECT cwopr.id,cwopr.amount,u.kingdee_code,u.phone,u.type,u.real_name,cwo.car_no
			FROM car_wash_order_pay_record cwopr
			LEFT JOIN car_wash_order cwo ON cwo.order_id = cwopr.order_id
			LEFT JOIN users u ON u.id = cwo.user_id
			WHERE cwo.`status` IN (1018, 1017, 1217, 1317, 1417)
			AND cwo.pay_time BETWEEN ? AND ?  AND cwo.shop_id = ?
			AND cwopr.pay_type = 8
			AND cwopr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        foreach ($c_amount as $_ca) {
        	$urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type = 2 group by uad.acount_type,aurr.shop_id", array($_ca->id));//获取这笔支付的完成需要几笔 充值支付记录
			$cur_type=$_ca->type==1?"个人":"企业";
			$cur_name=$_ca->real_name;
			$cur_code=$_ca->kingdee_code;			
			$car_no=$_ca->car_no;
			$user_type=$_ca->type;
			$flag=getlase($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code);
			if(!$flag){
				continue 3; //跳过该天所有门店的
			}			
        }
        //套餐卡现金收入
        $s_amount=DB::select("SELECT scopr.id,scopr.amount,u.kingdee_code,u.phone,u.type,u.real_name,sco.car_no
			FROM service_card_order_pay_record scopr
			LEFT JOIN service_card_order sco ON sco.order_id = scopr.order_id
			LEFT JOIN users u ON u.id = sco.user_id
			WHERE sco.`status` IN (1018, 1017, 1217, 1317, 1417)
			AND sco.pay_time BETWEEN ? AND ?  AND sco.shop_id = ?
			AND scopr.pay_type = 8
			AND scopr.`status` = 1", array($begin_date, $end_date,$_mdb->id));
        foreach ($s_amount as $_sa) {
        	$urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type = 8 group by uad.acount_type,aurr.shop_id", array($_sa->id));//获取这笔支付的完成需要几笔 充值支付记录
			$cur_type=$_sa->type==1?"个人":"企业";
			$cur_name=$_sa->real_name;
			$cur_code=$_sa->kingdee_code;			
			$car_no=$_sa->car_no;
			$user_type=$_sa->type;
			$flag=getlase($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code);
			if(!$flag){
				continue 3; //跳过该天所有门店的
			}			
        }   
        // //应收收回的逻辑
        $receive_amount=DB::select("SELECT rpr.id,rpr.amount,u.kingdee_code,u.phone,u.type,u.real_name,'' as car_no
			FROM receivable_pay_record rpr
			LEFT JOIN receivable r ON r.receivable_id = rpr.receivable_id
			LEFT JOIN users u ON u.id = r.company_id
			WHERE  r.verify_time BETWEEN ? AND ?  AND r.shop_id = ?
			and r.`verify_status`=1
			and r.`status`=1 
			AND rpr.pay_type = 8
			AND rpr.`status` = 1", array($begin_date,$end_date,$_mdb->id));
        foreach ($receive_amount as $_ra) {
        	$urr_amount=DB::select("SELECT uad.acount_type,aurr.shop_id,ms.kingdee_shopnum,ms.kingdee_bxcode,ms.name,sum(aursr.amount) as amount,aursr.implement_time
            FROM acount_user_recharge_spend_record aursr
            LEFT JOIN acount_user_recharge_record aurr ON aurr.id = aursr.aurr_id
            LEFT JOIN maintenance_shops ms on ms.id= aurr.shop_id
            LEFT JOIN user_account_details uad on uad.account_no=aurr.account_no
            WHERE aursr.buss_id = ? AND aursr.type IN(5,7) group by uad.acount_type,aurr.shop_id", array($_ra->id));//获取这笔支付的完成需要几笔 充值支付记录
			$cur_type=$_ra->type==1?"个人":"企业";
			$cur_name=$_ra->real_name;
			$cur_code=$_ra->kingdee_code;			
			$car_no=$_ra->car_no;
			$user_type=$_ra->type;
			$flag=getlaseRec($shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code);
			if(!$flag){
				continue 3; //跳过该天所有门店的
			}			
        }
    }
    //每天的门店都完成了在推送所有门店的 这样门店的凭证都合并
    //
    //合并 5501 和 科目备注相同的科目
    foreach($shop_array as &$ot){        
        if(sizeof($ot)==0){
            continue;
        }
        $temp_shop=&$ot;
        $depe_temp_bx=null;
        $depe_temp_xj=null;
        foreach($temp_shop as $key=>$t){
            if($t['FAccountID']=='5101'&&$t['FExplanation'] =="冲减保险预收款收入"){
                if(is_null($depe_temp_bx)){
                    $depe_temp_bx=$t;
                }else{
                    $depe_temp_bx["FAmountFor"]+=$t['FAmountFor'];
                    $depe_temp_bx["FAmount"]+=$t['FAmount'];
                }
                unset($temp_shop[$key]);
            }else if($t['FAccountID']=='5101'&&$t['FExplanation'] =="冲减预收款收入"){
                if(is_null($depe_temp_xj)){
                    $depe_temp_xj=$t;
                }else{
                    $depe_temp_xj["FAmountFor"]+=$t['FAmountFor'];
                    $depe_temp_xj["FAmount"]+=$t['FAmount'];
                }
                unset($temp_shop[$key]);
            }
        }
        if(!is_null($depe_temp_bx)){
            $temp_shop[]=$depe_temp_bx;
        }
        if(!is_null($depe_temp_xj)){
            $temp_shop[]=$depe_temp_xj;
        }
        $ot=$temp_shop;
        
    }
    

    foreach ($shop_array as $key=>$sa) {
    	if(sizeof($sa)==0){     
	        continue;
	    }
    	$temp_mdb=MaintenanceShops::find($key);
    	if(!$temp_mdb->kingdee_groupid){
            Log::error("门店 ".$temp_mdb->id."金蝶科目部门异常");
            continue 2; //跳过该天所有门店的
        } 
    	assembleNotsort($data,$sa,$date,$temp_mdb);//==================================================================
    }

    // Log::info($data);
    if(sizeof($data["mainVoucher"])>0){  
        getMaintain($data,$date);
        Log::info("门店".$_mdb->id." 日期 ".$date.'完毕');
        echo "门店".$_mdb->id." 日期 ".$date.'完毕';
    }
}


/**
 * @Author   lyt                      <ahlyt@anhuibj.com>
 * @DateTime 2018-07-03T15:34:59+0800
 * 用于奖一堆凭证详情拼接成一个凭证 并最终加入 最终的凭证结构体中
 */
function assembleNotsort(&$finally,$value_array,$date,$_mdb){
    if(sizeof($value_array)==0){     
        return $finally;
    }
    $temp =array(
        "FDate"=>$date." 00:00:00",
        "FGroupID"=>$_mdb->kingdee_groupid,//来广营 6
        "FReference"=>null,
        "FAttachments"=>0,
        "FEntryCount"=>4,
        "FDebitTotal"=>0,
        "FCreditTotal"=>0,
        "FPreparerID"=>16428,//写死的制单人
        "FPosterID"=>-1,
        "FTranType"=>0,
        "FTransDate"=>$date." 00:00:00",
        "entryVoucher"=>array(
            
        )           
    );
    $FEntryCount=0;
    $FDebitTotal=0;
    $FCreditTotal=0;
    foreach ($value_array as $value) {  
        $FEntryCount++;
        $value["FAmountFor"]=round($value["FAmountFor"],2);
        $value["FAmount"]=round($value["FAmount"],2);
        if($value["FDC"]==1){
            $FDebitTotal+=$value["FAmountFor"];
        }else{
            $FCreditTotal+=$value["FAmountFor"];
        }                           
        $temp["entryVoucher"][]=$value;
    }
    $temp["FEntryCount"]=$FEntryCount;
    $temp["FDebitTotal"]=round($FDebitTotal,2);
    $temp["FCreditTotal"]=round($FCreditTotal,2);
    $finally["mainVoucher"][]=$temp;
    return $finally;
}


//用于冲减本店的保险预存款
function assemLiushuiBX(&$data,$amount,$inputjie,$inputdai,$cur_type,$car_no,$shop_code,$shop_bxcode){   
    if($amount>0){
        $inputjie["FAccountID"]="2181.12";
        $inputjie["FExplanation"]=$cur_type." ".$car_no." 保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减保险预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$shop_code,"017"=>$shop_bxcode);
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于应收 冲减本店的保险预存款
function assemLiushuiBXRe(&$data,$amount,$inputjie,$inputdai,$cur_type,$cur_code,$shop_code,$shop_bxcode,$cur_name){   
    if($amount>0){
        $inputjie["FAccountID"]="2181.12";
        $inputjie["FExplanation"]=$cur_type." ".$cur_name." 冲减保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减保险预收款";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$shop_code,"017"=>$shop_bxcode);
        $datedai=array("001"=>$cur_code,"012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店预存款本店的账套
function assemLiushuiOWerBX(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]=$date." ".$cur_type." ".$car_no." 冲减 ".$other_shop_name." 保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减保险预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于收回使用别店预存款本店的账套
function assemLiushuiOWerBXRe(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$cur_code,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){  
    if($amount>0){
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."保险预收款"; 
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."保险预收款"; 
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("001"=>$cur_code,"012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店预存款别店的账套
function assemLiushuiOtherBX(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code,$other_shop_bxcode){   
    if($amount>0){
        $inputjie["FAccountID"]="2181.12"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 保险预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$other_shop_code,"017"=>$other_shop_bxcode);
        $datedai=array("012"=>$other_shop_code);
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}

//用于收回 使用别店预存款别店的账套
function assemLiushuiOtherBXRe(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code,$other_shop_bxcode){   
    if($amount>0){
        $inputjie["FAccountID"]="2181.12"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 冲减保险预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 冲减保险预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$other_shop_code,"017"=>$other_shop_bxcode);
        $datedai=array("012"=>$other_shop_code);
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}
//用于冲减本店的个人现金预存款
function assemLiushuiXHGR(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$shop_code){   
    if($amount>0){
        $inputjie["FAccountID"]="2131";
        $inputjie["FExplanation"]=$date." ".$cur_type." ".$car_no." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>184,"012"=>$shop_code);
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}

//用于使用别店现金个人本店的账套
function assemLiushuiOWerXHGR(&$data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]=$date." ".$cur_type." ".$car_no." 冲减 ".$other_shop_name." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店现金个人别店的账套
function assemLiushuiOtherXHGR(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code){   
    if($amount>0){
        $inputjie["FAccountID"]="2131"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;//使用门店的 往来代码
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_type." ".$car_no." 预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>184,"012"=>$other_shop_code);
        $datedai=array("012"=>$other_shop_code);//预存门店的 门店代码
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}


//用于冲减本店的企业现金预存款
function assemLiushuiXHQY(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$shop_code){   
    if($amount>0){
        $inputjie["FAccountID"]="2131";
        $inputjie["FExplanation"]=$date." ".$cur_name." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$shop_code);
        $datedai=array("012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于应收 冲减本店企业现金预存款
function assemLiushuiXHQYRe(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$shop_code){   
    if($amount>0){
        $inputjie["FAccountID"]="2131";
        $inputjie["FExplanation"]=$date." ".$cur_name." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$shop_code);
        $datedai=array("001"=>$cur_code,"012"=>$shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}

//用于使用别店现金企业本店的账套
function assemLiushuiOWerXHQY(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]=$date." ".$cur_name." ".$car_no." 冲减 ".$other_shop_name." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="5101";
        $inputdai["FExplanation"]="冲减预收款收入";  
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于应收 使用别店现金企业本店的账套
function assemLiushuiOWerXHQYRe(&$data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$other_shop_name,$ower_shop_code,$other_jie_code){   
    if($amount>0){
        $inputjie["FAccountID"]=$other_jie_code;
        $inputjie["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."预收款"; 
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]="1131";
        $inputdai["FExplanation"]="收回".$cur_name."欠款 冲减".$other_shop_name."预收款"; 
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("012"=>$ower_shop_code);
        $datedai=array("001"=>$cur_code,"012"=>$ower_shop_code);
        
        $inputjie["data"]=$datejie;
        $data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $data[]=$inputdai;
    }
}
//用于使用别店现金企业别店的账套
function assemLiushuiOtherXHQY(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code){   
    if($amount>0){
        $inputjie["FAccountID"]="2131"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;//使用门店的 往来代码
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$other_shop_code);
        $datedai=array("012"=>$other_shop_code);//预存门店的 门店代码
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}
//用于收回 使用别店现金企业别店的账套
function assemLiushuiOtherXHQYRe(&$other_data,$amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$ower_shop_name,$other_shop_code,$ower_jie_code){   
    if($amount>0){
        $inputjie["FAccountID"]="2131"; 
        $inputjie["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 冲减预收款";
        $inputjie["FAmountFor"]=round($amount/100,2);
        $inputjie["FAmount"]=round($amount/100,2);

        $inputdai["FAccountID"]=$ower_jie_code;//使用门店的 往来代码
        $inputdai["FExplanation"]=$date." ".$ower_shop_name."冲减".$cur_name." ".$car_no." 冲减预收款";
        $inputdai["FAmountFor"]=round($amount/100,2);
        $inputdai["FAmount"]=round($amount/100,2);

        $datejie=array("001"=>$cur_code,"012"=>$other_shop_code);
        $datedai=array("012"=>$other_shop_code);//预存门店的 门店代码
        
        $inputjie["data"]=$datejie;
        $other_data[]=$inputjie;      
        $inputdai["data"]=$datedai;
        $other_data[]=$inputdai;
    }
}

function getlase(&$shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code){//$urr_amount 要求结果集结构一致
	foreach ($urr_amount as $_ua) {		
		$date=date("Y-m-d",strtotime($_ua->implement_time));		
		if($_ua->shop_id==$_mdb->id){//说明本门店使用
			if($_ua->acount_type==2){//如果是使用的保险预存
				assemLiushuiBX($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$cur_type,$car_no,$_mdb->kingdee_shopnum,$_mdb->kingdee_bxcode);
			}else{//如果使用现金
				if ($user_type==1) {//个人 
					assemLiushuiXHGR($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->kingdee_shopnum);
				}else{
					assemLiushuiXHQY($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$_mdb->kingdee_shopnum);
				}
			}  
		}else{//说明跨门店使用
			$usr_msk=MaintenanceShopKingdee::where("shop_id",$_ua->shop_id)->where("code","1133.01")->first();//本门店的现金账套
	        if(is_null($usr_msk)){
	            Log::error("门店 ".$_ua->shop_id."内部往来科目异常");
	            return false;
	        }
			if($_ua->acount_type==2){//如果是使用的保险预存	
				assemLiushuiOWerBX($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
				assemLiushuiOtherBX($shop_array[$_ua->shop_id],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount,$_ua->kingdee_bxcode);
			}else{
				if($user_type==1) {//个人 
					assemLiushuiOWerXHGR($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
					assemLiushuiOtherXHGR($shop_array[$_ua->shop_id],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount);
				}else{
					assemLiushuiOWerXHQY($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
					assemLiushuiOtherXHQY($shop_array[$_ua->shop_id],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount);
				}
			}
		}
	}
	return true;
}

function getlaseRec(&$shop_array,$urr_amount,$_mdb,$inputjie,$inputdai,$cur_type,$user_type,$cur_name,$car_no,$ower_msk,$cur_code){//$urr_amount 要求结果集结构一致
    foreach ($urr_amount as $_ua) {     
        $date=date("Y-m-d",strtotime($_ua->implement_time));        
        if($_ua->shop_id==$_mdb->id){//说明本门店使用
            if($_ua->acount_type==2){//如果是使用的保险预存
                assemLiushuiBXRe($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$cur_type,$cur_code,$_mdb->kingdee_shopnum,$_mdb->kingdee_bxcode,$cur_name);
            }else{//如果使用现金              
                assemLiushuiXHQYRe($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$_mdb->kingdee_shopnum);
            }  
        }else{//说明跨门店使用
            $usr_msk=MaintenanceShopKingdee::where("shop_id",$_ua->shop_id)->where("code","1133.01")->first();//本门店的现金账套
            if(is_null($usr_msk)){
                Log::error("门店 ".$_ua->shop_id."内部往来科目异常");
                return false;
            }
            if($_ua->acount_type==2){//如果是使用的保险预存 
                assemLiushuiOWerBXRe($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$cur_code,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
                assemLiushuiOtherBXRe($shop_array[$_ua->shop_id],$_ua->amount,$inputjie,$inputdai,$date,$cur_type,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount,$_ua->kingdee_bxcode);
            }else{                
                assemLiushuiOWerXHQYRe($shop_array[$_mdb->id],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$_ua->name,$_mdb->kingdee_shopnum,$usr_msk->acount);
                assemLiushuiOtherXHQYRe($shop_array[$_ua->shop_id],$_ua->amount,$inputjie,$inputdai,$date,$cur_name,$cur_code,$car_no,$_mdb->name,$_ua->kingdee_shopnum,$ower_msk->acount);
                
            }
        }
    }
    return true;
}



function getMaintain($data,$date){ 
    $request = array(
        'token' => 1,
        'zhangtao' => "sqlsrv_scs",
        'params' => json_encode($data),
    );

    $response = Httpful::post(Config::get('config.kingdee_local_api')."/kingdeescs/importVoucher")
        ->timeoutIn(15)
        ->body($request)
        ->sendsForm()->send();

    $response = json_decode($response->body);
    Log::info($response->msg);//
    // $kvs=new KingdeeVoucherStatus();//该类型凭证是运行所有指定的
    // $kvs->date=$date;
    // $kvs->shop_id=$shop_id;
    // $kvs->type="liushui_ye";
    // if($response->code==0){//说明成功
    //     $kvs->status=1;
    // }else{
    //     $kvs->status=0;
    // }
    // $kvs->save();
}